home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / elf.c < prev    next >
C/C++ Source or Header  |  1994-10-21  |  12KB  |  440 lines

  1. /* ELF executable support for BFD.
  2.    Copyright 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of BFD, the Binary File Descriptor library.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  
  22. SECTION
  23.     ELF backends
  24.  
  25.     BFD support for ELF formats is being worked on.
  26.     Currently, the best supported back ends are for sparc and i386
  27.     (running svr4 or Solaris 2).
  28.  
  29.     Documentation of the internals of the support code still needs
  30.     to be written.  The code is changing quickly enough that we
  31.     haven't bothered yet.
  32.  */
  33.  
  34. #include "bfd.h"
  35. #include "sysdep.h"
  36. #include "bfdlink.h"
  37. #include "libbfd.h"
  38. #define ARCH_SIZE 0
  39. #include "libelf.h"
  40.  
  41. /* Standard ELF hash function.  Do not change this function; you will
  42.    cause invalid hash tables to be generated.  (Well, you would if this
  43.    were being used yet.)  */
  44. unsigned long
  45. bfd_elf_hash (name)
  46.      CONST unsigned char *name;
  47. {
  48.   unsigned long h = 0;
  49.   unsigned long g;
  50.   int ch;
  51.  
  52.   while ((ch = *name++) != '\0')
  53.     {
  54.       h = (h << 4) + ch;
  55.       if ((g = (h & 0xf0000000)) != 0)
  56.     {
  57.       h ^= g >> 24;
  58.       h &= ~g;
  59.     }
  60.     }
  61.   return h;
  62. }
  63.  
  64. /* Read a specified number of bytes at a specified offset in an ELF
  65.    file, into a newly allocated buffer, and return a pointer to the
  66.    buffer. */
  67.  
  68. static char *
  69. elf_read (abfd, offset, size)
  70.      bfd * abfd;
  71.      long offset;
  72.      int size;
  73. {
  74.   char *buf;
  75.  
  76.   if ((buf = bfd_alloc (abfd, size)) == NULL)
  77.     {
  78.       bfd_set_error (bfd_error_no_memory);
  79.       return NULL;
  80.     }
  81.   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
  82.     return NULL;
  83.   if (bfd_read ((PTR) buf, size, 1, abfd) != size)
  84.     {
  85.       if (bfd_get_error () != bfd_error_system_call)
  86.     bfd_set_error (bfd_error_file_truncated);
  87.       return NULL;
  88.     }
  89.   return buf;
  90. }
  91.  
  92. boolean
  93. elf_mkobject (abfd)
  94.      bfd * abfd;
  95. {
  96.   /* this just does initialization */
  97.   /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
  98.   elf_tdata (abfd) = (struct elf_obj_tdata *)
  99.     bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
  100.   if (elf_tdata (abfd) == 0)
  101.     {
  102.       bfd_set_error (bfd_error_no_memory);
  103.       return false;
  104.     }
  105.   /* since everything is done at close time, do we need any
  106.      initialization? */
  107.  
  108.   return true;
  109. }
  110.  
  111. char *
  112. elf_get_str_section (abfd, shindex)
  113.      bfd * abfd;
  114.      unsigned int shindex;
  115. {
  116.   Elf_Internal_Shdr **i_shdrp;
  117.   char *shstrtab = NULL;
  118.   unsigned int offset;
  119.   unsigned int shstrtabsize;
  120.  
  121.   i_shdrp = elf_elfsections (abfd);
  122.   if (i_shdrp == 0 || i_shdrp[shindex] == 0)
  123.     return 0;
  124.  
  125.   shstrtab = i_shdrp[shindex]->rawdata;
  126.   if (shstrtab == NULL)
  127.     {
  128.       /* No cached one, attempt to read, and cache what we read. */
  129.       offset = i_shdrp[shindex]->sh_offset;
  130.       shstrtabsize = i_shdrp[shindex]->sh_size;
  131.       shstrtab = elf_read (abfd, offset, shstrtabsize);
  132.       i_shdrp[shindex]->rawdata = (PTR) shstrtab;
  133.     }
  134.   return shstrtab;
  135. }
  136.  
  137. char *
  138. elf_string_from_elf_section (abfd, shindex, strindex)
  139.      bfd * abfd;
  140.      unsigned int shindex;
  141.      unsigned int strindex;
  142. {
  143.   Elf_Internal_Shdr *hdr;
  144.  
  145.   if (strindex == 0)
  146.     return "";
  147.  
  148.   hdr = elf_elfsections (abfd)[shindex];
  149.  
  150.   if (!hdr->rawdata
  151.       && elf_get_str_section (abfd, shindex) == NULL)
  152.     return NULL;
  153.  
  154.   return ((char *) hdr->rawdata) + strindex;
  155. }
  156.  
  157. /* Make a BFD section from an ELF section.  We store a pointer to the
  158.    BFD section in the rawdata field of the header.  */
  159.  
  160. boolean
  161. _bfd_elf_make_section_from_shdr (abfd, hdr, name)
  162.      bfd *abfd;
  163.      Elf_Internal_Shdr *hdr;
  164.      const char *name;
  165. {
  166.   asection *newsect;
  167.   flagword flags;
  168.  
  169.   if (hdr->rawdata != NULL)
  170.     {
  171.       BFD_ASSERT (strcmp (name, ((asection *) hdr->rawdata)->name) == 0);
  172.       return true;
  173.     }
  174.  
  175.   newsect = bfd_make_section_anyway (abfd, name);
  176.   if (newsect == NULL)
  177.     return false;
  178.  
  179.   newsect->filepos = hdr->sh_offset;
  180.  
  181.   if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
  182.       || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
  183.       || ! bfd_set_section_alignment (abfd, newsect,
  184.                       bfd_log2 (hdr->sh_addralign)))
  185.     return false;
  186.  
  187.   flags = SEC_NO_FLAGS;
  188.   if (hdr->sh_type != SHT_NOBITS)
  189.     flags |= SEC_HAS_CONTENTS;
  190.   if ((hdr->sh_flags & SHF_ALLOC) != 0)
  191.     {
  192.       flags |= SEC_ALLOC;
  193.       if (hdr->sh_type != SHT_NOBITS)
  194.     flags |= SEC_LOAD;
  195.     }
  196.   if ((hdr->sh_flags & SHF_WRITE) == 0)
  197.     flags |= SEC_READONLY;
  198.   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
  199.     flags |= SEC_CODE;
  200.   else if ((flags & SEC_LOAD) != 0)
  201.     flags |= SEC_DATA;
  202.  
  203.   /* The debugging sections appear to be recognized only by name, not
  204.      any sort of flag.  */
  205.   if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
  206.       || strncmp (name, ".line", sizeof ".line" - 1) == 0
  207.       || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
  208.     flags |= SEC_DEBUGGING;
  209.  
  210.   if (! bfd_set_section_flags (abfd, newsect, flags))
  211.     return false;
  212.  
  213.   hdr->rawdata = (PTR) newsect;
  214.   elf_section_data (newsect)->this_hdr = *hdr;
  215.  
  216.   return true;
  217. }
  218.  
  219. /*
  220. INTERNAL_FUNCTION
  221.     bfd_elf_find_section
  222.  
  223. SYNOPSIS
  224.     struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
  225.  
  226. DESCRIPTION
  227.     Helper functions for GDB to locate the string tables.
  228.     Since BFD hides string tables from callers, GDB needs to use an
  229.     internal hook to find them.  Sun's .stabstr, in particular,
  230.     isn't even pointed to by the .stab section, so ordinary
  231.     mechanisms wouldn't work to find it, even if we had some.
  232. */
  233.  
  234. struct elf_internal_shdr *
  235. bfd_elf_find_section (abfd, name)
  236.      bfd * abfd;
  237.      char *name;
  238. {
  239.   Elf_Internal_Shdr **i_shdrp;
  240.   char *shstrtab;
  241.   unsigned int max;
  242.   unsigned int i;
  243.  
  244.   i_shdrp = elf_elfsections (abfd);
  245.   if (i_shdrp != NULL)
  246.     {
  247.       shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
  248.       if (shstrtab != NULL)
  249.     {
  250.       max = elf_elfheader (abfd)->e_shnum;
  251.       for (i = 1; i < max; i++)
  252.         if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
  253.           return i_shdrp[i];
  254.     }
  255.     }
  256.   return 0;
  257. }
  258.  
  259. const char *const bfd_elf_section_type_names[] = {
  260.   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
  261.   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
  262.   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
  263. };
  264.  
  265. /* ELF relocs are against symbols.  If we are producing relocateable
  266.    output, and the reloc is against an external symbol, and nothing
  267.    has given us any additional addend, the resulting reloc will also
  268.    be against the same symbol.  In such a case, we don't want to
  269.    change anything about the way the reloc is handled, since it will
  270.    all be done at final link time.  Rather than put special case code
  271.    into bfd_perform_relocation, all the reloc types use this howto
  272.    function.  It just short circuits the reloc if producing
  273.    relocateable output against an external symbol.  */
  274.  
  275. /*ARGSUSED*/
  276. bfd_reloc_status_type
  277. bfd_elf_generic_reloc (abfd,
  278.                reloc_entry,
  279.                symbol,
  280.                data,
  281.                input_section,
  282.                output_bfd,
  283.                error_message)
  284.      bfd *abfd;
  285.      arelent *reloc_entry;
  286.      asymbol *symbol;
  287.      PTR data;
  288.      asection *input_section;
  289.      bfd *output_bfd;
  290.      char **error_message;
  291. {
  292.   if (output_bfd != (bfd *) NULL
  293.       && (symbol->flags & BSF_SECTION_SYM) == 0
  294.       && (! reloc_entry->howto->partial_inplace
  295.       || reloc_entry->addend == 0))
  296.     {
  297.       reloc_entry->address += input_section->output_offset;
  298.       return bfd_reloc_ok;
  299.     }
  300.  
  301.   return bfd_reloc_continue;
  302. }
  303.  
  304. /* Display ELF-specific fields of a symbol.  */
  305. void
  306. bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
  307.      bfd *ignore_abfd;
  308.      PTR filep;
  309.      asymbol *symbol;
  310.      bfd_print_symbol_type how;
  311. {
  312.   FILE *file = (FILE *) filep;
  313.   switch (how)
  314.     {
  315.     case bfd_print_symbol_name:
  316.       fprintf (file, "%s", symbol->name);
  317.       break;
  318.     case bfd_print_symbol_more:
  319.       fprintf (file, "elf ");
  320.       fprintf_vma (file, symbol->value);
  321.       fprintf (file, " %lx", (long) symbol->flags);
  322.       break;
  323.     case bfd_print_symbol_all:
  324.       {
  325.     CONST char *section_name;
  326.     section_name = symbol->section ? symbol->section->name : "(*none*)";
  327.     bfd_print_symbol_vandf ((PTR) file, symbol);
  328.     fprintf (file, " %s\t", section_name);
  329.     /* Print the "other" value for a symbol.  For common symbols,
  330.        we've already printed the size; now print the alignment.
  331.        For other symbols, we have no specified alignment, and
  332.        we've printed the address; now print the size.  */
  333.     fprintf_vma (file,
  334.              (bfd_is_com_section (symbol->section)
  335.               ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
  336.               : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
  337.     fprintf (file, " %s", symbol->name);
  338.       }
  339.       break;
  340.     }
  341. }
  342.  
  343. /* Create an entry in an ELF linker hash table.  */
  344.  
  345. struct bfd_hash_entry *
  346. _bfd_elf_link_hash_newfunc (entry, table, string)
  347.      struct bfd_hash_entry *entry;
  348.      struct bfd_hash_table *table;
  349.      const char *string;
  350. {
  351.   struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
  352.  
  353.   /* Allocate the structure if it has not already been allocated by a
  354.      subclass.  */
  355.   if (ret == (struct elf_link_hash_entry *) NULL)
  356.     ret = ((struct elf_link_hash_entry *)
  357.        bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
  358.   if (ret == (struct elf_link_hash_entry *) NULL)
  359.     {
  360.       bfd_set_error (bfd_error_no_memory);
  361.       return (struct bfd_hash_entry *) ret;
  362.     }
  363.  
  364.   /* Call the allocation method of the superclass.  */
  365.   ret = ((struct elf_link_hash_entry *)
  366.      _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  367.                  table, string));
  368.   if (ret != (struct elf_link_hash_entry *) NULL)
  369.     {
  370.       /* Set local fields.  */
  371.       ret->indx = -1;
  372.       ret->size = 0;
  373.       ret->dynindx = -1;
  374.       ret->dynstr_index = 0;
  375.       ret->weakdef = NULL;
  376.       ret->got_offset = (bfd_vma) -1;
  377.       ret->plt_offset = (bfd_vma) -1;
  378.       ret->type = STT_NOTYPE;
  379.       ret->elf_link_hash_flags = 0;
  380.     }
  381.  
  382.   return (struct bfd_hash_entry *) ret;
  383. }
  384.  
  385. /* Initialize an ELF linker hash table.  */
  386.  
  387. boolean
  388. _bfd_elf_link_hash_table_init (table, abfd, newfunc)
  389.      struct elf_link_hash_table *table;
  390.      bfd *abfd;
  391.      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
  392.                         struct bfd_hash_table *,
  393.                         const char *));
  394. {
  395.   table->dynamic_sections_created = false;
  396.   table->dynobj = NULL;
  397.   /* The first dynamic symbol is a dummy.  */
  398.   table->dynsymcount = 1;
  399.   table->dynstr = NULL;
  400.   table->bucketcount = 0;
  401.   return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
  402. }
  403.  
  404. /* Create an ELF linker hash table.  */
  405.  
  406. struct bfd_link_hash_table *
  407. _bfd_elf_link_hash_table_create (abfd)
  408.      bfd *abfd;
  409. {
  410.   struct elf_link_hash_table *ret;
  411.  
  412.   ret = ((struct elf_link_hash_table *)
  413.      bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
  414.   if (ret == (struct elf_link_hash_table *) NULL)
  415.     {
  416.       bfd_set_error (bfd_error_no_memory);
  417.       return NULL;
  418.     }
  419.  
  420.   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
  421.     {
  422.       bfd_release (abfd, ret);
  423.       return NULL;
  424.     }
  425.  
  426.   return &ret->root;
  427. }
  428.  
  429. /* This is a hook for the ELF emulation code in the generic linker to
  430.    tell the backend linker what file name to use for the DT_NEEDED
  431.    entry for a dynamic object.  */
  432.  
  433. void
  434. bfd_elf_set_dt_needed_name (abfd, name)
  435.      bfd *abfd;
  436.      const char *name;
  437. {
  438.   elf_dt_needed_name (abfd) = name;
  439. }
  440.